//
//  JFAlertView.m
//  弹窗哈哈哈
//
//  Created by Alan Wang on 2018/11/8.
//  Copyright © 2018 kuaijiankang. All rights reserved.
//

#import "JFAlertView.h"
#import <JFBase/JFBase.h>
#import "JFIpoAlertBottomBottomView.h"
#import "JFAlertTextView.h"
#import "JFKitPri.h"

@implementation JFAlertView
+(JFAlertView*)alertViewWithTitle:(NSString*)title message:(NSString*)message preferredStyle:(JFAlertViewStyle)style{
    JFAlertView * alert = [[JFAlertView alloc]init];
    alert.title = title;
    alert.message = message;
    alert.style = style;
    if(style == JFAlertViewStyleCustomActionSheet || style == JFAlertViewStyleActionSheet){
        alert.showSplitLine = YES;
    }
    return alert;
}
+(JFAlertView*)alertViewWithTitle:(NSString*)title message:(NSString*)message tips:(NSString*)tips  preferredStyle:(JFAlertViewStyle)style{
    JFAlertView * alert = [[JFAlertView alloc]init];
    alert.title = title;
    alert.message = message;
    alert.tips = tips;
    alert.style = style;
    if(style == JFAlertViewStyleCustomActionSheet || style == JFAlertViewStyleActionSheet){
        alert.showSplitLine = YES;
    }
    return alert;
}
+(JFAlertView*)alertViewWithNewStyleTitle:(NSString*)title  subTitle:(NSString*)subTitle message:(id)message cancle:(NSString*)cancle sure:(NSString*)sure action:(void(^)(NSInteger type,id data))actionBlock{
         JFAlertView * alert = [JFAlertView alertViewWithTitle:nil message:nil preferredStyle:JFAlertViewStyleCustomViewOnly];
          alert.clickBackGroundHide = NO;
          CGFloat y = 0;
          CGFloat width = 315;
          CGFloat margin = 15;
          CGFloat titleHeight = 64;
          CGFloat bottomBtnHeight = 92;
          UIView * containerView = [self containerView];
          UILabel * titleLab = [self titleLab];
          titleLab.text = title;
          y+=titleHeight;
          [containerView addSubview:titleLab];
          AWMasFast(titleLab, {
              AWMasLeft(containerView, 0)
              AWMasRight(containerView, 0)
              AWMasTop(containerView, 0)
              AWMasHeight(titleHeight)
          })
          UILabel * subTitleLab = nil;
         if ([JFStringHelper isNotEmpty:subTitle]) {
               CGFloat subTitleLabHeight = 18;
               UILabel * subTitleLab = [[UILabel alloc] init];
             subTitleLab = subTitleLab;
           subTitleLab.AWfont(JFScalePingFangFont(13)).AWtextColor(titleLab.textColor).AWtextAlignment(NSTextAlignmentCenter);
                subTitleLab.text =subTitle;
              [containerView addSubview:subTitleLab];
              AWMasFast(subTitleLab, {
                  AWMasLeft(containerView, 0)
                  AWMasRight(containerView, 0)
                  AWMasTop(titleLab.mas_bottom, 0)
                AWMasHeight(titleHeight)
              })
             y+=subTitleLabHeight;
         }

        CGFloat  contentHeight = 0;
        JFAlertTextView * textView = [[JFAlertTextView alloc] initWithFrame:CGRectZero];
        textView.textBlock = ^(NSString * _Nonnull txt) {
           actionBlock(3,txt);
        };
        if ([message isKindOfClass:[NSString class]]) {
             NSString * tempContentStr = (NSString*)message;
             CGFloat tempContentStrHeight = [JFStringHelper getHeightLineWithString:tempContentStr lineSpacing:0 withWidth:width-2*margin withFont:textView.textView.font]+24;
              contentHeight = tempContentStrHeight;
              textView.textView.text = tempContentStr;
       }else if ([message isKindOfClass:[NSAttributedString class]]){
           NSString * tempContentStr = ((NSAttributedString*)message).string;
           CGFloat tempContentStrHeight = [JFStringHelper getHeightLineWithString:tempContentStr lineSpacing:0 withWidth:width-2*margin withFont:textView.textView.font]+24;
           contentHeight = tempContentStrHeight ;
           textView.textView.attributedText = message;
       }
       if (contentHeight > 250) {
          contentHeight = 250;
       }
      
       UIView * tempView = [[UIView alloc] init];
       tempView.backgroundColor = AWThemeColorForKey(@"Market.ipoAlertCellBgColor");
      [tempView addSubview:textView];
      AWMasFast(textView, {
                      AWMasLeft(tempView, margin)
                      AWMasRight(tempView, -margin)
                      AWMasTop(tempView, 0)
                     AWMasHeight(contentHeight)
      })
       
    
       [containerView addSubview:tempView];
       
    
       if(subTitleLab){
         AWMasFast(tempView, {
                      AWMasLeft(containerView, margin)
                      AWMasRight(containerView, -margin)
                      AWMasTop(subTitleLab.mas_bottom, 0)
                    AWMasHeight(contentHeight)
                  })
       }else{
        AWMasFast(tempView, {
                     AWMasLeft(containerView, margin)
                     AWMasRight(containerView, -margin)
                     AWMasTop(titleLab.mas_bottom, 0)
                   AWMasHeight(contentHeight)
                 })
       }
       y+=margin;
       y+=contentHeight;
        
        JFIpoAlertBottomBottomView * bottomBtnView = [self bottomBtnView];
         bottomBtnView.leftBtnTitle = cancle;
         bottomBtnView.rightBtnTitle = sure;
         if([JFStringHelper isEmpty:cancle]){
            bottomBtnView.style = 1;
         }else{
           bottomBtnView.style = 0;
         }
        
    
         bottomBtnView.actionBlock = ^(NSInteger type) {
            [alert hide];
            if (actionBlock) {
                actionBlock(type,nil);
            }
         };
       [containerView addSubview:bottomBtnView];
       AWMasFast(bottomBtnView, {
                        AWMasLeft(containerView, 0)
                        AWMasRight(containerView, 0)
                        AWMasTop(textView.mas_bottom, margin)
                      AWMasHeight(bottomBtnHeight)
        })
         y+=margin;
         y+=bottomBtnHeight;
        containerView.frame = CGRectMake(0, 0, width, y);
        alert.customcontentView = containerView;
//        [alert showFromView:[JFJumpManage shareInstance].getCurrentVC.parentViewController.view];
        return alert;
}

-(void)addAction:(JFAlertAction *)action{
    if(action){
      if(action.tag == 0){
          action.tag = self.actionArr.count;
      }
     [self.actionArr addObject:action];
    }
}


//容器视图
+(UIView*)containerView{
      UIView * containerView = [[UIView alloc] init];
      containerView.backgroundColor = AWThemeColorForKey(@"Global.navBgColor");
    return containerView;
}
//标题视图
+(UILabel*)titleLab{
    UILabel * lab = [[UILabel alloc] init];
    lab.AWtextColor(AWThemeColorForKey(@"Global.navTintColor")).AWfont(JFPingFangFont(17)).AWtextAlignment(NSTextAlignmentCenter).AWbackgroundColor([UIColor clearColor]);
    return lab;
}
+(UILabel*)bottomDetailLab{
    UILabel * lab = [[UILabel alloc] init];
  lab.AWtextColor(AWThemeColorForKey(@"Market.iposubtitlecolor")).AWfont(JFScalePingFangFont(12)).AWbackgroundColor([UIColor clearColor]);
    lab.numberOfLines = 0;
    return lab;
}
+(JFIpoAlertBottomBottomView*)bottomBtnView{
    JFIpoAlertBottomBottomView * bottomBtnView = [[JFIpoAlertBottomBottomView alloc] init];
    return bottomBtnView;
}
@end
